package cn.wit.job.service.impl;

import cn.wit.job.condition.SearchCompanyCondition;
import cn.wit.job.mapper.CompanyMapper;
import cn.wit.job.mapper.RecruitInfoMapper;
import cn.wit.utils.pojo.Company;
import cn.wit.utils.pojo.RecruitInfo;
import cn.wit.job.service.CompanyService;
import cn.wit.job.service.RecruitInfoService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.apache.poi.ss.usermodel.CellType.BLANK;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private RecruitInfoMapper recruitInfoMapper;

    @Autowired
    private RecruitInfoService recruitInfoService;
    @Override
    public Result insert(Company c) {
        int i = companyMapper.insert(c);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
        //如果RecruitInfo中没有依赖该Id的数据，那么可以删除，否则不给删除,根据外键查询
        QueryWrapper<RecruitInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("company_ID", id);
        List<RecruitInfo> recruitInfos = recruitInfoMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(recruitInfos)){
            int i = companyMapper.deleteById(id);
            if(i > 0){
                return Result.ok();
            }
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(Company c) {
        int i = companyMapper.updateById(c);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("更新失败");
    }


    @Override
    public Result selectAllCompany() {
        List<Company> companies = companyMapper.selectList(null);
        if(!CollectionUtils.isEmpty(companies)){
            return Result.ok(companies);
        }
        return Result.ok(new ArrayList<>());
    }

    @Override
    public Result searchPage(IPage<Company> page, SearchCompanyCondition condition) {
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        if(condition != null) {
            if(condition.getName() != null && condition.getName() != "") {
                wrapper.like("name", condition.getName());
            }
            if(condition.getNature() != null && condition.getNature() != "") {
                wrapper.like("nature", condition.getNature());
            }
            if(condition.getIsAppear() != null && condition.getIsAppear() != "") {
                wrapper.like("isAppear", condition.getIsAppear());
            }
        }
        IPage<Company> companyIPage = companyMapper.selectPage(page, wrapper);
        return Result.ok(companyIPage);
    }

    @Override
    public Result excelImport(MultipartFile file) {
        InputStream inputStream = null;
        int num=0;
        try {
            inputStream = file.getInputStream();
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int length = sheet.getLastRowNum()+1;
            int numRows = sheet.getPhysicalNumberOfRows();
            for (int i = 1; i < numRows; i++) {//自动跳过标题头
                Row row = sheet.getRow(i);
                Company company = new Company();
                String name = row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//公司名
                if(row.getCell(0).getCellType()!= CellType.BLANK && name!= null &&  name!= ""){
                    company.setName(name);
                }
                String nature = row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//公司性质
                if(row.getCell(1).getCellType()!=CellType.BLANK && nature!=null && nature!=""){
                    company.setNature(nature);
                }
                String isAppear = row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//是否上市
                if(row.getCell(2).getCellType()!=CellType.BLANK && isAppear!=null && isAppear!=""){
                    company.setIsAppear(isAppear);
                }
                String introduction=row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//公司介绍
                if(introduction!=null&&introduction!=""){
                    company.setIntroduction(introduction);
                }
                String link = row.getCell(4, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();//链接
                if(link!=null && link!=""){
                    company.setLink(link);
                }
                int count = companyMapper.insert(company);
                num+=count;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(num>0){
            return Result.ok();
        }else {
            return Result.error("excel导入失败");
        }
    }
}
